package com.tcfvic.client.panels.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.tcfvic.client.PersonDetailsListService;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.PersonDetailsForm;
import com.tcfvic.client.panels.widgets.addons.advancedtable.AdvancedTable;
import com.tcfvic.client.panels.widgets.addons.advancedtable.DataFilter;
import com.tcfvic.client.panels.widgets.addons.advancedtable.TableModelServiceAsync;

public class PersonDetailsListWidget extends FlexTable {
	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);
	private TextBox firstNameTextBox = new TextBox();
	private TextBox surnameTextBox = new TextBox();
	private TextBox companyTextBox = new TextBox();
	private TextBox siteTextBox = new TextBox();
	private AdvancedTable at = null;

	public PersonDetailsListWidget(
			TableModelServiceAsync tableModelServiceAsync,
			final PersonDetailsInputWidget rowSelectionListener,
			final PersonDetailsForm personDetailsForm) {
		super();
		at = new AdvancedTable();

		// Create a table to layout the form options
		setCellSpacing(4);
		setWidth("100%");
		FlexCellFormatter cellFormatter = getFlexCellFormatter();

		// Add a title to the form
		at.setFirstColumnVisible(false);
		at.setPageSize(9);
		at.setHeight("300 px");
		at.setWidth("100%");

		at.addRowSelectionListener(rowSelectionListener);

		// last action to be performed
		at.setTableModelService(tableModelServiceAsync);

		firstNameTextBox.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter(
							PersonDetailsListService.column1Name, filter
									.getText());
					df[1] = new DataFilter(
							PersonDetailsListService.column2Name,
							surnameTextBox.getText());
					df[2] = new DataFilter(
							PersonDetailsListService.column3Name,
							companyTextBox.getText());
					df[3] = new DataFilter(
							PersonDetailsListService.column4Name, siteTextBox
									.getText());
					at.applyFilters(df);
				}
			}
		});

		surnameTextBox.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter(
							PersonDetailsListService.column1Name,
							firstNameTextBox.getText());
					df[1] = new DataFilter(
							PersonDetailsListService.column2Name, filter
									.getText());
					df[2] = new DataFilter(
							PersonDetailsListService.column3Name,
							companyTextBox.getText());
					df[3] = new DataFilter(
							PersonDetailsListService.column4Name, siteTextBox
									.getText());
					at.applyFilters(df);
				}
			}
		});

		companyTextBox.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter(
							PersonDetailsListService.column1Name,
							firstNameTextBox.getText());
					df[1] = new DataFilter(
							PersonDetailsListService.column2Name,
							surnameTextBox.getText());
					df[2] = new DataFilter(
							PersonDetailsListService.column3Name, filter
									.getText());
					df[3] = new DataFilter(
							PersonDetailsListService.column4Name, siteTextBox
									.getText());
					at.applyFilters(df);
				}
			}
		});

		siteTextBox.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter(
							PersonDetailsListService.column1Name,
							firstNameTextBox.getText());
					df[1] = new DataFilter(
							PersonDetailsListService.column2Name,
							surnameTextBox.getText());
					df[2] = new DataFilter(
							PersonDetailsListService.column3Name,
							companyTextBox.getText());
					df[3] = new DataFilter(
							PersonDetailsListService.column4Name, filter
									.getText());
					at.applyFilters(df);
				}
			}
		});

		// create new record button
		Button newButton = new Button("Create new");
		newButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				rowSelectionListener.createNewRecord();
			}
		});

		// create new record button
		final Button deleteButton = new Button("");
		personService.isAdminUser(new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					deleteButton.setText("Delete");
					deleteButton.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							personService.deletePerson(Integer.parseInt(at
									.getSelectedRowId()),
									new AsyncCallback<Void>() {
										@Override
										public void onFailure(Throwable caught) {
											new SimpleMessagePopup(
													"The request to delete the person failed.",
													null).showPopup();
										}

										@Override
										public void onSuccess(Void result) {
											at.updateTableData();
										}
									});
						}
					});
				} else {
					deleteButton.setVisible(false);
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(newButton);
		hp.add(deleteButton);
		setWidget(0, 0, hp);

		hp = new HorizontalPanel();
		HTML label = new HTML("Surname");
		hp.add(label);
		hp.add(surnameTextBox);
		label = new HTML("Given name");
		hp.add(label);
		hp.add(firstNameTextBox);
		label = new HTML("Company");
		hp.add(label);
		hp.add(companyTextBox);
		label = new HTML("Site");
		hp.add(label);
		hp.add(siteTextBox);
		setWidget(1, 0, hp);

		setWidget(2, 0, at);
		cellFormatter.setColSpan(2, 0, 6);

		addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				personDetailsForm.getvSplit().setSplitPosition("50%");
			}
		});

	}

	public void updateTableData() {
		at.updateTableData();
	}
}
